<?php
class ControllerModuleCartDetailView extends Controller { 
    protected function index() {      
        $this->data['template'] = $this->config->get('config_template');
        $this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
        $this->data['loadproduct'] = HTTPS_SERVER . 'index.php?route=module/cart_detailview/loadProduct'; 
        $this->data['text_error'] = $this->language->get('text_error');
        if($this->cart->hasProducts()){             
        	$this->data['heading_title'] = $this->language->get('heading_title');
        		
        	$this->data['text_select'] = $this->language->get('text_select');
        	$this->data['text_sub_total'] = $this->language->get('text_sub_total');
        	$this->data['text_discount'] = $this->language->get('text_discount');
        	$this->data['text_weight'] = $this->language->get('text_weight');
        	
        	$this->data['column_remove'] = $this->language->get('column_remove');
        	$this->data['column_image'] = $this->language->get('column_image');
        	$this->data['column_name'] = $this->language->get('column_name');
        	$this->data['column_model'] = $this->language->get('column_model');
        	$this->data['column_quantity'] = $this->language->get('column_quantity');
        	$this->data['column_price'] = $this->language->get('column_price');
        	$this->data['column_total'] = $this->language->get('column_total');
        	
        	$this->data['button_update'] = $this->language->get('button_update');
        	$this->data['button_shopping'] = $this->language->get('button_shopping');
        	$this->data['button_checkout'] = $this->language->get('button_checkout');
        		
        	if (isset($this->error['warning'])) {
        		$this->data['error_warning'] = $this->error['warning'];
        	} elseif (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
        		$this->data['error_warning'] = $this->language->get('error_stock');
        	} else {
        		$this->data['error_warning'] = '';
        	}
        	
        	$this->data['action'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
        		
        	$this->load->model('tool/seo_url');
        	$this->load->model('tool/image');
        	$this->load->model('catalog/product');
        		
        	$this->data['products'] = array();
        	$this->data['giftcards'] = array();
        	
        	foreach ($this->cart->getProducts() as $result) {
        		$option_data = array();
        		foreach ($result['option'] as $option) {
        			$option_data[] = array(
        					'name'              => $option['name'],
        					'value'             => $option['value'],
        					'product_number'    => $option['product_number'],
        			);
        		}
        	
        		$size = $this->model_catalog_product->getSizeOption($result['option']);
        		$size = isset($size['value']) ? $size['value'] : '';
        	
        		$color = $this->model_catalog_product->getColorOption($result['product_id'], $result['option']);
        		$color_name = isset($color['color_name']) ? $color['color_name'] : '';
        		$color_id = isset($color['product_color_id']) ? $color['product_color_id'] : '';
        		 
        		if(isset($color['image']) && false){
        			$image = $color['image'];
        		}
        		elseif ($result['image']) {
        			$image = $result['image'];
        		}
        		else {
        			$image = 'no_image.jpg';
        		}
        	
        		//$short_name = (strlen($result['name']) > 26) ? (substr($result['name'], 0, 26) . ' ...') : $result['name'];
        		$short_name = $this->model_catalog_product->productShortName($result['name'], 26);
        		$this->data['products'][] = array(
        				'key'      	=> $result['key'],
        				'name'     	=> $result['name'],
        				'shortname' => $short_name,
        				'model'    	=> $result['model'],
        				//'thumb'    	=> $this->model_tool_image->resize($image, 61, 75),
        				'thumb'     => $this->model_tool_image->resize($image, 100, 115),
        				'option'   	=> $option_data,
        				'quantity' 	=> $result['quantity'],
        				'stock'    	=> $result['stock'],
        				'price'    	=> $this->currency->formatPrice($result['price']),
        				'total'    	=> $this->currency->formatPrice($result['total']),
        				'href'     	=> $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']),
        				'size'		=> $size,
        				'color'		=> $color_name,
        				'color_id'	=> $color_id,
        				'subtotals' => $this->currency->formatPrice($result['quantity'] * $result['price']),
        				'product_number' => $result['sku'],
        		);
        	}
        	
        	// Gift card
        	if (isset($this->session->data['giftcards']) && $this->session->data['giftcards']) {
        		foreach ($this->session->data['giftcards'] as $key => $giftcard) {
        			$this->data['giftcards'][] = array(
        					'key'         	=> $key,
        					'description' 	=> $giftcard['description'],
        					'amount'      	=> $this->currency->formatPrice($giftcard['amount']),
        					'thumb'     	=> $this->model_tool_image->resize('giftcard.jpg', 100, 115),
        			);
        		}
        	}
        		
        	if (!$this->config->get('config_customer_price')) {
        		$this->data['display_price'] = TRUE;
        	} elseif ($this->customer->isLogged()) {
        		$this->data['display_price'] = TRUE;
        	} else {
        		$this->data['display_price'] = FALSE;
        	}
        		
        	if ($this->config->get('config_cart_weight')) {
        		$this->data['weight'] = $this->weight->format($this->cart->getWeight(), $this->config->get('config_weight_class'));
        	} else {
        		$this->data['weight'] = FALSE;
        	}
        		
        	$total_data = array();
        	$total = 0;
        	$taxes = $this->cart->getTaxes();
        		
        	$this->load->model('checkout/extension');
        		
        	$sort_order = array();
        		
        	$results = $this->model_checkout_extension->getExtensions('total');
        		
        	foreach ($results as $key => $value) {
        		$sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
        	}
        		
        	array_multisort($sort_order, SORT_ASC, $results);
        		
        	foreach ($results as $result) {
        		$this->load->model('total/' . $result['key']);
        	
        		$this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
        	}
        		
        	$sort_order = array();
        	
        	foreach ($total_data as $key => $value) {
        		$sort_order[$key] = $value['sort_order'];
        	}
        	
        	array_multisort($sort_order, SORT_ASC, $total_data);
        	
        	/*
        	 $subtotals = $this->cart->getSubTotal();
        	$shipping_value = ($this->config->get('flat_free_shipping') >= $subtotals) ? $this->config->get('flat_cost') : 0;
        	$tax_value = 0;
        	foreach($taxes as $item){
        	$tax_value += $item;
        	}
        	$this->data['tax_value'] = $this->currency->formatPrice($tax_value);
        	$this->data['subtotals'] = $this->currency->formatPrice($subtotals);
        	$this->data['totals'] = $this->currency->formatPrice($subtotals + $shipping_value);
        	$this->data['shipping_cost'] = $this->currency->formatPrice($shipping_value);
        	$this->session->data['subtotals'] = $subtotals;
        	*/
        	if(isset($total_data['shipping'])){
        		$shipping_value = $this->currency->formatPrice($total_data['shipping']['value']);
        	}
        	else{
        		if($this->cart->getSubTotal() > 0){
        			$shipping_value = ($this->config->get('flat_free_shipping') >= $total_data['subtotal']['value']) ? $this->currency->formatPrice($this->config->get('flat_cost')) : 0;
        		}
        		else{
        			$shipping_value = 0;
        		}
        	}
        	
        	if(isset($total_data['tax'])){
        		$tax_name = $total_data['tax']['title'];
        		$tax_value = $this->currency->formatPrice($total_data['tax']['value']);
        	}
        	else{
        		$tax_name = 'Moms:'; //'Moms udgør:';
        		$tax_value = '0% moms';
        	}
        	
        	$this->data['tax_name'] = $tax_name;
        	$this->data['tax_value'] = $tax_value;
        	$this->data['subtotals'] = $this->currency->formatPrice($total_data['subtotal']['value']);
        	$this->data['totals'] = $this->currency->formatPrice($total_data['total']['value']);
        	$this->data['shipping_cost'] = $shipping_value;
        	
        	$this->session->data['subtotals'] = $total_data['subtotal']['value'];
        	
        	if(isset($this->session->data['go_back_product'])){
        		$this->data['go_back'] = $this->session->data['go_back_product'];
        	}
        	else{
        		$this->data['go_back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
        	}
        	if(isset($this->session->data['go_back_category'])){
        		$this->data['go_back_category'] = $this->session->data['go_back_category'];
        	}
        	else{
        		$this->data['go_back_category'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
        	}
        	if (isset($this->session->data['redirect'])) {
        		$this->data['continue'] = $this->session->data['redirect'];
        		unset($this->session->data['redirect']);
        	}
        	else {
        		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
        	}
        	//$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_1';
        	$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/onestep';
            $this->data['view']     = HTTP_SERVER . 'index.php?route=checkout/cart';
        		
        	if(!$this->config->get('config_stock_checkout')){
        		if(isset($this->session->data['in_stock'])){
        			$this->data['in_stock'] = $this->session->data['in_stock'];
        		}
        		else{
        			$this->data['in_stock'] = $this->cart->hasStock();
        		}
        	}
        	else{
        		$this->data['in_stock'] = true;
        	}
        	unset($this->session->data['in_stock']);
        }
        else{ 
            $this->data['products'] = null;   
        }

        $this->id = 'cart_detailview';
            
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cart_detailview.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/cart_detailview.tpl';    
        } else {
            $this->template = 'default/template/module/cart_detailview.tpl';
        }
         
        $this->render();
    }
    
    public function loadProduct() {
        $this->index();
        $this->response->setOutput($this->output, $this->config->get('config_compression'));

        return;


        $this->data['template'] = $this->config->get('config_template');
        $this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
        $this->data['loadproduct'] = HTTPS_SERVER . 'index.php?route=module/cart_detailview/loadProduct'; 
        $this->data['text_error'] = $this->language->get('text_error');
        if($this->cart->hasProducts()){             
        	$this->data['heading_title'] = $this->language->get('heading_title');
        		
        	$this->data['text_select'] = $this->language->get('text_select');
        	$this->data['text_sub_total'] = $this->language->get('text_sub_total');
        	$this->data['text_discount'] = $this->language->get('text_discount');
        	$this->data['text_weight'] = $this->language->get('text_weight');
        	
        	$this->data['column_remove'] = $this->language->get('column_remove');
        	$this->data['column_image'] = $this->language->get('column_image');
        	$this->data['column_name'] = $this->language->get('column_name');
        	$this->data['column_model'] = $this->language->get('column_model');
        	$this->data['column_quantity'] = $this->language->get('column_quantity');
        	$this->data['column_price'] = $this->language->get('column_price');
        	$this->data['column_total'] = $this->language->get('column_total');
        	
        	$this->data['button_update'] = $this->language->get('button_update');
        	$this->data['button_shopping'] = $this->language->get('button_shopping');
        	$this->data['button_checkout'] = $this->language->get('button_checkout');
        		
        	if (isset($this->error['warning'])) {
        		$this->data['error_warning'] = $this->error['warning'];
        	} elseif (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
        		$this->data['error_warning'] = $this->language->get('error_stock');
        	} else {
        		$this->data['error_warning'] = '';
        	}
        	
        	$this->data['action'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
        		
        	$this->load->model('tool/seo_url');
        	$this->load->model('tool/image');
        	$this->load->model('catalog/product');
        		
        	$this->data['products'] = array();
        	$this->data['giftcards'] = array();
        	
        	foreach ($this->cart->getProducts() as $result) {
        		$option_data = array();
        		foreach ($result['option'] as $option) {
        			$option_data[] = array(
        					'name'              => $option['name'],
        					'value'             => $option['value'],
        					'product_number'    => $option['product_number'],
        			);
        		}
        	
        		$size = $this->model_catalog_product->getSizeOption($result['option']);
        		$size = isset($size['value']) ? $size['value'] : '';
        	
        		$color = $this->model_catalog_product->getColorOption($result['product_id'], $result['option']);
        		$color_name = isset($color['color_name']) ? $color['color_name'] : '';
        		$color_id = isset($color['product_color_id']) ? $color['product_color_id'] : '';
        		 
        		if(isset($color['image']) && false){
        			$image = $color['image'];
        		}
        		elseif ($result['image']) {
        			$image = $result['image'];
        		}
        		else {
        			$image = 'no_image.jpg';
        		}
        	
        		//$short_name = (strlen($result['name']) > 26) ? (substr($result['name'], 0, 26) . ' ...') : $result['name'];
        		$short_name = $this->model_catalog_product->productShortName($result['name'], 26);
        		$this->data['products'][] = array(
        				'key'      	=> $result['key'],
        				'name'     	=> $result['name'],
        				'shortname' => $short_name,
        				'model'    	=> $result['model'],
        				//'thumb'    	=> $this->model_tool_image->resize($image, 61, 75),
        				'thumb'     => $this->model_tool_image->resize($image, 100, 115),
        				'option'   	=> $option_data,
        				'quantity' 	=> $result['quantity'],
        				'stock'    	=> $result['stock'],
        				'price'    	=> $this->currency->formatPrice($result['price']),
        				'total'    	=> $this->currency->formatPrice($result['total']),
        				'href'     	=> $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']),
        				'size'		=> $size,
        				'color'		=> $color_name,
        				'color_id'	=> $color_id,
        				'subtotals' => $this->currency->formatPrice($result['quantity'] * $result['price']),
        				'product_number' => $result['sku'],
        		);
        	}
        	
        	// Gift card
        	if (isset($this->session->data['giftcards']) && $this->session->data['giftcards']) {
        		foreach ($this->session->data['giftcards'] as $key => $giftcard) {
        			$this->data['giftcards'][] = array(
        					'key'         	=> $key,
        					'description' 	=> $giftcard['description'],
        					'amount'      	=> $this->currency->formatPrice($giftcard['amount']),
        					'thumb'     	=> $this->model_tool_image->resize('giftcard.jpg', 100, 115),
        			);
        		}
        	}
        		
        	if (!$this->config->get('config_customer_price')) {
        		$this->data['display_price'] = TRUE;
        	} elseif ($this->customer->isLogged()) {
        		$this->data['display_price'] = TRUE;
        	} else {
        		$this->data['display_price'] = FALSE;
        	}
        		
        	if ($this->config->get('config_cart_weight')) {
        		$this->data['weight'] = $this->weight->format($this->cart->getWeight(), $this->config->get('config_weight_class'));
        	} else {
        		$this->data['weight'] = FALSE;
        	}
        		
        	$total_data = array();
        	$total = 0;
        	$taxes = $this->cart->getTaxes();
        		
        	$this->load->model('checkout/extension');
        		
        	$sort_order = array();
        		
        	$results = $this->model_checkout_extension->getExtensions('total');
        		
        	foreach ($results as $key => $value) {
        		$sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
        	}
        		
        	array_multisort($sort_order, SORT_ASC, $results);
        		
        	foreach ($results as $result) {
        		$this->load->model('total/' . $result['key']);
        	
        		$this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
        	}
        		
        	$sort_order = array();
        	
        	foreach ($total_data as $key => $value) {
        		$sort_order[$key] = $value['sort_order'];
        	}
        	
        	array_multisort($sort_order, SORT_ASC, $total_data);
        	
        	/*
        	 $subtotals = $this->cart->getSubTotal();
        	$shipping_value = ($this->config->get('flat_free_shipping') >= $subtotals) ? $this->config->get('flat_cost') : 0;
        	$tax_value = 0;
        	foreach($taxes as $item){
        	$tax_value += $item;
        	}
        	$this->data['tax_value'] = $this->currency->formatPrice($tax_value);
        	$this->data['subtotals'] = $this->currency->formatPrice($subtotals);
        	$this->data['totals'] = $this->currency->formatPrice($subtotals + $shipping_value);
        	$this->data['shipping_cost'] = $this->currency->formatPrice($shipping_value);
        	$this->session->data['subtotals'] = $subtotals;
        	*/
        	if(isset($total_data['shipping'])){
        		$shipping_value = $this->currency->formatPrice($total_data['shipping']['value']);
        	}
        	else{
        		if($this->cart->getSubTotal() > 0){
        			$shipping_value = ($this->config->get('flat_free_shipping') >= $total_data['subtotal']['value']) ? $this->currency->formatPrice($this->config->get('flat_cost')) : 0;
        		}
        		else{
        			$shipping_value = 0;
        		}
        	}
        	
        	if(isset($total_data['tax'])){
        		$tax_name = $total_data['tax']['title'];
        		$tax_value = $this->currency->formatPrice($total_data['tax']['value']);
        	}
        	else{
        		$tax_name = 'Moms:'; //'Moms udgør:';
        		$tax_value = '0% moms';
        	}
        	
        	$this->data['tax_name'] = $tax_name;
        	$this->data['tax_value'] = $tax_value;
        	$this->data['subtotals'] = $this->currency->formatPrice($total_data['subtotal']['value']);
        	$this->data['totals'] = $this->currency->formatPrice($total_data['total']['value']);
        	$this->data['shipping_cost'] = $shipping_value;
        	
        	$this->session->data['subtotals'] = $total_data['subtotal']['value'];
        	
        	if(isset($this->session->data['go_back_product'])){
        		$this->data['go_back'] = $this->session->data['go_back_product'];
        	}
        	else{
        		$this->data['go_back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
        	}
        	if(isset($this->session->data['go_back_category'])){
        		$this->data['go_back_category'] = $this->session->data['go_back_category'];
        	}
        	else{
        		$this->data['go_back_category'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
        	}
        	if (isset($this->session->data['redirect'])) {
        		$this->data['continue'] = $this->session->data['redirect'];
        		unset($this->session->data['redirect']);
        	}
        	else {
        		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
        	}
        	//$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_1';
        	$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/onestep';
            $this->data['view']     = HTTP_SERVER . 'index.php?route=checkout/cart';

        	if(!$this->config->get('config_stock_checkout')){
        		if(isset($this->session->data['in_stock'])){
        			$this->data['in_stock'] = $this->session->data['in_stock'];
        		}
        		else{
        			$this->data['in_stock'] = $this->cart->hasStock();
        		}
        	}
        	else{
        		$this->data['in_stock'] = true;
        	}
        	unset($this->session->data['in_stock']);
        }
        else{ 
            $this->data['products'] = null;   
        }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cart_detailview_ajax.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/cart_detailview_ajax.tpl';    
        } 
        else {
            $this->template = 'default/template/module/cart_detailview_ajax.tpl';
        }
        
        $this->response->setOutput($this->render(true), $this->config->get('config_compression')); 
    }
}
?>